var antlr4 = require('antlr4');
var PL00Lexer = require('../antlr/PL00Lexer').PL00Lexer;
var PL00Parser = require('../antlr/PL00Parser').PL00Parser;
/**
 * 导入模块函数的工厂类
 * @param {PL00ParserVisitor} visitor 访问器
 * @returns 根据模块名异步导入模块的函数
 */
function importModuleFactory(visitor) {
    return (moduleName) => {
        return fetch('https://xyqlx.gitee.io/pl00-modules/' + moduleName + '.pl0')
            .then(response => response.text())
            .then((data) => {
                const chars = new antlr4.InputStream(data)
                const lexer = new PL00Lexer(chars)
                const tokens = new antlr4.CommonTokenStream(lexer)
                const parser = new PL00Parser(tokens)
                parser.buildParserTrees = true;
                const tree = parser.functionDefs();
                const functions = visitor.visitFunctionDefs(tree);
                return functions;
            }).catch((e) => {
                return null;
            })
    }
}
exports.importModuleFactory = importModuleFactory;